package wqh.controller.json;

import cn.tpson.hk.core.HKDTServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import wqh.domain.HkServer;
import wqh.hk_server.HkServerCallBack;
import wqh.pojo.MapRet;
import wqh.service.HkServerService;

import java.util.List;

/**
 * Created by wqh on 2017/1/16.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/hk_server")
public class HkServerJSON {

    @Autowired
    private HkServerService hkServerService;

    @Autowired
    private HkServerCallBack hkServerCallBack;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody HkServer hkServer){
        try {
            hkServerService.save(hkServer);
            HKDTServer.getInstance().setCallBack(hkServerCallBack);
            HKDTServer.getInstance().addServer(hkServer.getIp(),hkServer.getPort(),-1);
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody HkServer hkServer){
        try {
            HkServer h = hkServerService.findById(hkServer.getId());
            if(h == null) return MapRet.SQL_UPDATE_ERROR();
            if(hkServer.getPort() != null) h.setPort(hkServer.getPort());
            if(hkServer.getIp() != null) h.setIp(hkServer.getIp());
            if(hkServer.getConnection_try() != null) h.setConnection_try(hkServer.getConnection_try());
            hkServerService.update(h);
            if(hkServer.getPort() != null || hkServer.getIp() != null){
                List<HkServer> list = hkServerService.findAll();
                if(list.size() != 0){
                    HKDTServer.getInstance().setCallBack(hkServerCallBack);
                    HKDTServer.getInstance().stopServer();
                    Runnable runnable = () -> {
                        try {
                            for(HkServer hk : list){
                                HKDTServer.getInstance().addServer(hk.getIp(),hk.getPort(),hk.getConnection_try());
                                Thread.sleep(500);
                            }
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    };
                    Thread thread = new Thread(runnable);
                    thread.start();
                }
                }
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public Object delete(@RequestBody HkServer hkServer){
        try {
            HkServer h = hkServerService.findById(hkServer.getId());
            if(h == null) return MapRet.SQL_DELETE_ERROR();
            hkServerService.delete(hkServer);
            List<HkServer> list = hkServerService.findAll();
            if(list.size() != 0){
                HKDTServer.getInstance().setCallBack(hkServerCallBack);
                HKDTServer.getInstance().stopServer();
                Runnable runnable = () -> {
                    try {
                        for(HkServer hk : list){
                            HKDTServer.getInstance().addServer(hk.getIp(),hk.getPort(),hk.getConnection_try());
                            Thread.sleep(500);
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                };
                Thread thread = new Thread(runnable);
                thread.start();
            }
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_DELETE_ERROR();
        }
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(){
        return MapRet.DATA(hkServerService.findAll());
    }

}
